from selenium import  webdriver
import pyautogui
import pyperclip
from selenium.webdriver.common.by import By
from time import sleep
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

# 封装的意思：把相同类似的代码放到一个函数里面,重复使用
def wait_element_clickable(locator):
    """
    等待元素可被点击
    """
    # web_element代表通过显示等待找到的元素
    web_element = WebDriverWait(driver,8,0.5).until(EC.element_to_be_clickable(locator))
    return web_element

def wait_element_visible(locator):
    """
    等待元素可见
    """
    # web_element代表通过显示等待找到的元素
    web_element =  WebDriverWait(driver, 8, 0.5).until(EC.visibility_of_element_located(locator))
    return web_element

def wait_element_presence(locator):
    """
    等待元素存在
    """
    # web_element代表通过显示等待找到的元素
    web_element =  WebDriverWait(driver, 8, 0.5).until(EC.presence_of_element_located(locator))
    return web_element

driver = webdriver.Chrome()
driver.get('http://mall.lemonban.com:3344/')
driver.maximize_window()
# 1、登录的操作
# 登录
loc_login = (By.XPATH,'//a[text()="登录"]')
# 用户名输入框
loc_username = (By.XPATH,'//input[@placeholder="请输入手机号/用户名"]')
# 密码输入框
loc_password = (By.XPATH,'//input[@placeholder="请输入密码"]')
# 登录按钮
loc_login_but = (By.CLASS_NAME,'login-button')

wait_element_clickable(loc_login).click()
wait_element_visible(loc_username).send_keys('lemon_py')
wait_element_visible(loc_password).send_keys('12345678')
wait_element_clickable(loc_login_but).click()
sleep(2)  # 登录后一定要强制等待2s

# 2、点击个人中心
wait_element_clickable((By.XPATH,'//span[text()="个人中心"]')).click()

# 3、点击'图片'元素
wait_element_clickable((By.CLASS_NAME,'portrait')).click()
wait_element_clickable((By.CLASS_NAME,'avatar')).click()
sleep(1)

#4、传图片操作
pyperclip.copy(r'C:\Users\libin\Desktop\验证码.png')
pyautogui.hotkey("ctrl","v")
sleep(1)
pyautogui.press("enter",presses=2)

# 5、点击保存账户信息
wait_element_clickable((By.LINK_TEXT,'保存账户信息')).click()

#6、刷新浏览器，才可以看到最后的修改效果
driver.refresh()

sleep(2)
driver.quit()